# ##############################################################################
# components/communication/net_dev/cantrcv1145/CMakeLists.txt
#
# Copyright (c) 2025 Li Auto Inc. and its affiliates
# Licensed under the Apache License, Version 2.0(the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ##############################################################################

if(CONFIG_MODULE_CANTRCV1145)
    # 头文件引入
    set( INCLUDE_DIR
        # GENERAL
        ${NUTTX_APPS_DIR}/components/include/autosar/
        ${NUTTX_APPS_DIR}/components/include/
        ${GENDATA_DIR}/Os
        ${GENDATA_DIR}/Rte
        ${GENDATA_DIR}/ComM
        ${GENDATA_DIR}/Nm
        ${GENDATA_DIR}/CanNm
        ${GENDATA_DIR}/CanSM
        ${GENDATA_DIR}/CanIf
        ${GENDATA_DIR}/Com
        ${GENDATA_DIR}/PduR
        ${GENDATA_DIR}/CanTrcv1145
        ${GENDATA_DIR}/Memmap
        ${GENDATA_DIR}/NvM
        ${NUTTX_APPS_DIR}/${APPS_DIR}/apps
        ${NUTTX_APPS_DIR}/components/rt_framework/inc

        #vcos log
        ${NUTTX_APPS_DIR}/components/maintaining/vcos_log/inc
        #NM inc
        ${CMAKE_CURRENT_SOURCE_DIR}/inc
        #CanNM inc
        ${CMAKE_CURRENT_SOURCE_DIR}/../cannm/inc
        #ComM inc
        ${CMAKE_CURRENT_SOURCE_DIR}/../comm/inc
        #CanSM inc
        ${CMAKE_CURRENT_SOURCE_DIR}/../cansm/inc
        #CanIf inc
        ${CMAKE_CURRENT_SOURCE_DIR}/../../net_dev/canif/inc
        #Com inc
        ${CMAKE_CURRENT_SOURCE_DIR}/../../rt_com/inc
        #PduR inc
        ${CMAKE_CURRENT_SOURCE_DIR}/../../pdu_router/inc
        #NvM inc
        ${NUTTX_APPS_DIR}/components/memory/nvm/inc
    )

    if(CONFIG_ARCH_CHIP_E3650)
        list(APPEND INCLUDE_DIR
            ${CAN_STACK_E3XX_DIR}
        )
    endif()

    if(CONFIG_SERVM_BSWM)
        list(APPEND INCLUDE_DIR
            #BswM inc
            ${CMAKE_CURRENT_SOURCE_DIR}/../../../system_services/service_management/engin/inc
            ${GENDATA_DIR}/ServM/BswM
        )
    endif()

    if(CONFIG_SERVM_ECUM)
        list(APPEND INCLUDE_DIR
            #EcuM inc
            ${CMAKE_CURRENT_SOURCE_DIR}/../../../system_services/service_management/sysm_init/inc
            ${GENDATA_DIR}/sysm/EcuM
        )
    endif()

    if(CONFIG_ARCH_CHIP_J6E)
        list(APPEND INCLUDE_DIR ${CAN_STACK_J6E_DIR})
    endif()

    # 收集 CANTRCV1145 目录下的所需 .c 文件
    file(GLOB CANTRCV1145_SOURCES
        "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c"
    )

    # 合并所有源文件
    set( NET_CANTRCV1145_SRCS
        ${CANTRCV1145_SOURCES}
        ${GENDATA_DIR}/CanTrcv1145/cantrcv_tja1145_cfg.c
        ${GENDATA_DIR}/CanTrcv1145/cantrcv_tja1145_pbcfg.c
    )

    nuttx_add_library(net_cantrcv1145 STATIC)
    target_sources(net_cantrcv1145 PRIVATE ${NET_CANTRCV1145_SRCS})
    target_include_directories(net_cantrcv1145 PRIVATE ${INCLUDE_DIR})
    if (CONFIG_SIM_CAN_DRIVER)
        target_link_libraries(net_cantrcv1145 PRIVATE sim_can)
    elseif(CONFIG_ARCH_CHIP_CUSTOM_TC397)
        target_link_libraries(net_cantrcv1145 PRIVATE can_driver)
    elseif(CONFIG_ARCH_CHIP_E3650)
        target_link_libraries(net_cantrcv1145 PRIVATE e3_can_driver)
    elseif(CONFIG_ARCH_CHIP_J6E)
        target_link_libraries(net_cantrcv1145 PRIVATE j6_can_driver)
    endif()
endif()
